
*************************************************;
**  Output MA HMO data for matching with FFS   **;
*************************************************;

options ls=100 ps=1000 nofmterr;

libname new '/data/Medicare_P01_2009/data/HEDIS/sasdata';

data a0; set new.hedis2004;

    ***************************************************;
    **  Demographic Variables and Sample Selection   **;
    ***************************************************;

     if denom_flag=1;
     if state=' ' or state='00' or state > "53" or state in ("40","48") then delete;

     age=age+1; /** set age to end of year **/

     if age>=65;

     if plan_type='HMO - Health Maintenance Organization';

     length newagec $ 8;

     if age=65            then newagec='1.65';
     if age>65 and age<70 then newagec='2.66-69';
     if age>69 and age<75 then newagec='3.70-74';
     if age>74 and age<80 then newagec='4.75-79';
     if age>79 and age<85 then newagec='5.80-84';
     if age>84            then newagec='6.85+';      

     if racec=' ' then racec='0.Unknown';

     if age>65 and totmonth<12 then delete; 


     if dod='00000000' then dod=' ';

     deathyear=substr(dod,1,4);
     deathmon=substr(dod,5,2);

     if (vdod='V' or deathyear>0) then death1=1; else death1=0;

     if death1=1 and
       (deathyear=2004 or
       (deathyear=2005 and (deathmon>0 and deathmon<3)))
     then delete ;


    ******************************************;
    ** Recoding and Cleaning All Measures   **;
    ******************************************;

     ***** Quality Rates *****;

     array rate bcs_d bcs_n ch_ace_d ch_acen1 cdc_d cdc_n1 cdc_n3 cdc_n4 gso_d gso_n;  
     do over rate; 
        if rate=9 then rate=0; 
     end;

             *-- cleaning BCS --*;

     if sex=1 or age>69 then bcs_d=.;
     if bcs_d=. or bcs_d=0 then bcs_n=.; 

             *-- cleaning CMC --*;

     if ch_ace_d=0 then ch_acen1=.; 
 
             *-- cleaning CDC --*;

     if age>75 then cdc_d=.;
     if cdc_d=. or cdc_d=0 then do; cdc_n1=.; cdc_n3=.; cdc_n4=.; end;

             *-- cleaning GSO --*;

     if gso_d=0 then gso_n=.; 

             *-- Rename Some Variables and keep same var for all years --*;

     cmc_d = ch_ace_d;  label cmc_d = 'Denom for LDL-C Screening After AccuteCare';
     cmc_n1= ch_acen1;  label cmc_n1= 'Numer for LDL-C Screening After AccuteCare';


     *****  Frequent Selected Procedures  *****;

     array fps   fspcabgn fspptcan fsp_cc_n fsp_ce_n fsp_rffn fsp_thrn fsp_tkrn
                 fsp_xlin fsp_oc_n fsp_clcn fsp_ah_n fsp_vh_n fsp_p_n ;
     do over fps; 
        if fps=999 then fps=0;  
     end;

     array uti   acd_n_t  acd_s_n  acd_m_n acdy_n_t acdy_s_n acdy_m_n  
                 ac_ov_n  ac_erv_n ac_asp_n ac_ors_n ;
     do over uti;
        if uti=999 then uti=0;
     end;

            *-- Gender specific Procedures --*;

     if sex=1 then do; fsp_ah_n=.; fsp_vh_n=.; end;  /** female only **/
     if sex=2 then do; fsp_p_n=.;              end;  /** male only **/


     ***** CAP measures  John and Bruce email 6-4-2010 *****;

     if fspcabgn>3 then fspcabgn=3;   
     if fspptcan>4 then fspptcan=4;   
     if fsp_cc_n>4 then fsp_cc_n=4;   
     
     array fps2 fsp_ce_n fsp_rffn fsp_thrn fsp_tkrn fsp_xlin fsp_oc_n fsp_clcn fsp_ah_n fsp_vh_n fsp_p_n;
     do over fps2; 
          if fps2>2 then fps2=2;  
     end;
       
     if acd_n_t > 12 then acd_n_t = 12;   * Total discharges set max 1 per month * ;    
     if acd_s_n > 12 then acd_s_n = 12;   * Surgery discharges *;
     if acd_m_n > 12 then acd_m_n = 12;   * Medical discharges *;

     if acdy_n_t > 365 then acdy_n_t = 365;  * Maximum possible in 12 months *;
     if acdy_s_n > 365 then acdy_s_n = 365;
     if acdy_m_n > 365 then acdy_m_n = 365;

     if ac_ov_n  > 50 then ac_ov_n  = 50; * Tot Out Patient visit set max 1 per week *;
     if ac_erv_n > 12 then ac_erv_n = 12; * Set max 1 per month *;
     if ac_asp_n > 12 then ac_asp_n = 12; 
     if ac_ors_n > 12 then ac_ors_n = 12;
 

     ********************************************************;
     ** Rename Variables and keep consistent for all years **;
     ********************************************************;   

     n_cabg = fspcabgn ;  label n_cabg='CABG';
     n_ptca = fspptcan ;  label n_ptca='PTCA';
     n_cc   = fsp_cc_n ;  label n_cc  ='CardiacCathetherization';
     n_ce   = fsp_ce_n ;  label n_ce  ='CardiacEnarterectomy';
     n_rff  = fsp_rffn ;  label n_rff ='ReductionFemurFracture';
     n_thr  = fsp_thrn ;  label n_thr ='TotalHipReplacement';
     n_tkr  = fsp_tkrn ;  label n_tkr ='TotalKneereplacement';
     n_xli  = fsp_xlin ;  label n_xli ='ExcisionLargeIntest';
     n_oc   = fsp_oc_n ;  label n_oc  ='OpenCholecystectomy';
     n_clc  = fsp_clcn ;  label n_clc ='ClosedCholecstectomy';
     n_ah   = fsp_ah_n ;  label n_ah  ='AbdominalHysterectomy';
     n_vh   = fsp_vh_n ;  label n_vh  ='VaginalHysterectomy';
     n_p    = fsp_p_n ;   label n_p   ='Prostatectomy';

     iptotdis   = acd_n_t ;  label iptotdis ='InpatTotalDischarges';
     ipsurdis   = acd_s_n ;  label ipsurdis ='InpatSurgeryDischarges';
     ipmeddis   = acd_m_n ;  label ipmeddis ='InpatMedicineDischarges';

     iptotdays  = acdy_n_t ; label iptotdays ='InpatTotalDays';
     ipsurdays  = acdy_s_n ; label ipsurdays ='InpatSurgeryDays';
     ipmeddays  = acdy_m_n ; label ipmeddays ='InpatMedicineDays';

     amb_opv =  ac_ov_n ;   label amb_opv ='OutPatientVisits';
     amb_erv =  ac_erv_n ;  label amb_erv ='EmergencyRoomVisits';
     amb_asp =  ac_asp_n ;  label amb_asp ='AbmSugeryProcedures';
     amb_ors =  ac_ors_n ; label amb_ors ='ObservationRoomStays';


 data a; set a0;

     %let all_vars = bcs_d bcs_n cmc_d cmc_n1 cdc_d cdc_n1 cdc_n3 cdc_n4 gso_d gso_n
                     n_cabg n_ptca n_cc n_ce n_rff n_thr n_tkr n_xli n_oc n_clc n_ah n_vh n_p
                     iptotdis ipsurdis ipmeddis iptotdays ipsurdays ipmeddays amb_opv amb_erv amb_asp amb_ors m_months;
     
     proc sort; by newagec gender racec state org_id county zip5;

     proc means data=a n sum noprint;
           by newagec gender racec state org_id county zip5;
               var &all_vars;
               output out=allst0 n=  sum= /autoname;


  data new.hedis04hmo; set allst0;

         if _TYPE_ = 0;
         tot_n = _FREQ_ ;
         totmon = m_months_sum;

         drop _TYPE_ ;
         drop _FREQ_ ;
         drop m_months_n;
         drop m_months_sum;

         proc contents data=new.hedis04hmo varnum;
              title 'Content of HEDIS04hmo';

         proc print data=new.hedis04hmo(obs=5);

         proc freq; 
              tables org_id; 

endsas;
